//
//  GameViewController.m
//  BeatPlane
//
//  Created by qianfeng on 15-5-6.
//  Copyright (c) 2015年 韩世民. All rights reserved.
//

#import "GameViewController.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
#import "Plane.h"
#import "Bullet.h"
#import "Enemy.h"
#define kBulletNumber 30

@interface GameViewController ()
{
    //游戏界面
    UIImageView *_backImageView;
    Plane *_plane;
    //定时器
    NSTimer *_timer;
    UIView *_towardsView;
    NSMutableArray *_bulletArr;
    NSMutableArray *_enemyArr;
    UIImageView *_BOOMView;
    Enemy *_bigEnemy;
}

@end
//游戏界面
@implementation GameViewController
-(void)dealloc{
    
    [_enemyArr release];
    
    
    [_backImageView release];
    [_towardsView release];
    [super dealloc];
    [_bulletArr   release];
    [_BOOMView release];
    
}
/*
 1.创建背景
 2.设计飞机
 3.设计方向按钮 控制飞机移动
 4.飞机发射子弹
 5.设计敌人  敌人移动
 6.碰撞检测
 7....
 
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self gameInit];
    
}


#pragma mark -游戏初始化
//在这里写初始化方法
-(void)gameInit{
    [self creatGameView];
    //创建飞机
    [self creatPlane];
    [self creatTowardsView];
    [self creatBullets];
    [self creatEnemys];
}
#pragma mark  -界面将要显示的时候
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //创建定时器
    [self creatTimer];
    
}
//界面将要离开屏幕的时候调用
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if ([_timer isValid]) {//判断定时器是否有效
        [_timer invalidate];//终止定时器
        _timer =nil;
    }
}
#pragma mark -定时器
//定时器 控制器 飞机的移动 敌人的移动 子弹的移动 碰撞检测
-(void)creatTimer{
    if (_timer ) {
        [_timer invalidate];//
        _timer =nil;
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self  selector:@selector(timerClick) userInfo:nil repeats:YES];
    
}
//定时器 触发的方法
-(void)timerClick{
    
    [_plane planeAction];
    //子弹飞
    [self bulletFly];
    [self enemyMove];
    [self isHitting];
    [self creatanimation];
    
}

#pragma mark -碰撞检测
-(void)isHitting{
    for (Bullet *bullet in _bulletArr) {
        //子弹在屏幕外不用检测
        if (bullet.frame.origin.y <= - bullet.frame.size.height) {
            continue;//遍历下一个子弹
        }
        //遍历所有敌人
        for (Enemy *enemy in _enemyArr) {
            //碰撞检测
            //检测两个frame 有没有交集
            BOOL ret = CGRectIntersectsRect(bullet.frame, enemy.frame);
            if (ret) {
                
                _BOOMView.frame = enemy.frame;
                [enemy resetFrame];
                bullet.frame =CGRectMake(-50, -50, bullet.frame.size.width, bullet.frame.size.height);
                [_BOOMView startAnimating];
            }
        }
    }
    }
#pragma mark -爆炸效果
-(void)creatanimation{
    
    _BOOMView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
    
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (NSInteger i =0; i<6; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"bomb%ld.png",i]];
        [arr addObject:image];
    }
    _BOOMView.animationImages = arr;
    [arr release];
    _BOOMView.animationDuration=0.1;
    _BOOMView.animationRepeatCount = 1;
    [self.view  addSubview:_BOOMView];
}
#pragma mark -创建敌人
-(void)creatEnemys{
    _enemyArr = [[NSMutableArray alloc ] init];
    for (NSInteger i =0; i<10; i++) {
        Enemy *enemy = [[Enemy alloc] init];
        [self.view addSubview:enemy ];
        [_enemyArr addObject:enemy];
        [enemy release];
    }
}
/*
#pragma mark-创建老母
-(void)creatbigEnemy{
    _bigEnemy= [[Enemy alloc]init];
    NSMutableArray *arr1 = [[NSMutableArray alloc] init];
    for (NSInteger i =0; i<4; i++) {
        UIImage*image1 =[UIImage imageNamed:[NSString stringWithFormat:@"boss%ld.png",i]];
        [arr1 addObject:image1];
    }
    _bigEnemy.animationImages =arr1;
    [arr1 release];
    _bigEnemy.animationDuration =1;
    _bigEnemy.animationRepeatCount  = 10;
    [self.view addSubview:_bigEnemy];
}
 */
//所有敌人移动
-(void)enemyMove{
    for (Enemy *enemy in _enemyArr) {
        [enemy moveDown];
    }
}
#pragma mark -创建子弹
-(void)creatBullets{
    _bulletArr = [[NSMutableArray alloc] init];
    for (NSInteger i =0; i<kBulletNumber; i++) {
        //把子弹放在外边
        Bullet *bullet = [[Bullet alloc] initWithFrame:CGRectMake(-50, -50, 17, 17)];
        [self.view addSubview:bullet];
        [_bulletArr addObject:bullet];
        [bullet release];
        
    }
    
}
#pragma mark - 子弹飞
//定时器控制
-(void)bulletFly{
    //子弹循环利用
    static int i =0;
    
    if (_plane.isFire) {
        
        if (i % 5==0) {
    
        Bullet *bullet = _bulletArr[i/5];
        //重置到飞机的位置 发射位置
        [bullet resetFrame];
        }
        i++;
        
        if (i  ==kBulletNumber*5) {
            i = 0;
        }
    }
    
    
    for (Bullet *bullet in _bulletArr) {
        [bullet  moveup];
    }
}
#pragma mark - 创建方向按钮
-(void)creatTowardsView{
    _towardsView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenSize.height-50, 150, 50)];
    _towardsView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_towardsView];
    //创建方向按钮
    NSArray *imageNames = @[@"button_left",@"button_right"];
    for (NSInteger i =0; i<imageNames.count; i++) {
        UIButton  *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame =CGRectMake(100*i, 0, 50, 50);
        [button setBackgroundImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
        button.tag = 301+i;
        //增加时间
        [button addTarget:self action:@selector(changeToward:) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(stopPlane) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(stopPlane) forControlEvents:UIControlEventTouchUpOutside];
        [_towardsView addSubview:button];
    }
    //创建发射按钮
    UIButton *fireButton = [UIButton buttonWithType:UIButtonTypeCustom];
    fireButton.frame = CGRectMake(50, 0, 50, 50);
    [fireButton setBackgroundImage:[UIImage imageNamed:@"fire.png"] forState:UIControlStateNormal];
    [fireButton addTarget:self action:@selector(fireButton:) forControlEvents:UIControlEventTouchUpInside];
    [_towardsView addSubview:fireButton];
    
}
-(void)fireButton:(UIButton*)button{
    if (_plane.isFire) {
        //判断是否正在发子弹
        _plane.isFire =NO;
        [button setBackgroundImage:[UIImage imageNamed:@"fire"] forState:UIControlStateNormal];
    }else{
        _plane.isFire =YES;
        [button setBackgroundImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
        
    }
}
#pragma mark -方向按钮控制方法
-(void)changeToward:(UIButton *)button{
    switch (button.tag) {
        case 301:
            [_plane startMoveLeft];
            
            break;
        case 302:
            [_plane startMoveRight];
            
            break;
            
        default:
            break;
    }
}
-(void)stopPlane{
    [_plane stopMoveing];
}
#pragma mark -创建飞机
-(void)creatPlane{
    
    _plane = [Plane defaultPlane];
    
    [self.view addSubview:_plane];
}
#pragma mark - 背景设置
-(void)creatGameView{
    _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height)];
    _backImageView.image = [UIImage imageNamed:@"backgroud.png"];
    /*
     CGRect frame = _backImageView.frame;
    if (frame.origin.y > kScreenSize.height) {
        frame.origin.y = 0;
        _backImageView.frame = frame;
   }else{
        frame.origin.y+=5;
        _backImageView.frame =frame;
    }
    
     */
    [self.view addSubview:_backImageView];
    
}
     
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
